* Reset settings and initialize log file
launch, path("share/timeuse")

*-------------------------------------------------------------------------------
* Price and Wasserman (2024), "The Summer Drop in Female Employment"
*
* Description: Analyze seasonal patterns in mothers' and fathers' time use.
*-------------------------------------------------------------------------------


* Prepare data and run models
*-------------------------------------------------------------------------------

if "$estimate" != "0" {
	* Load ATUS data
	use "$basepath/data/derived/atus.dta", clear
	tsset caseid tm

	* Estimate seasonal patterns in childcare time
	foreach f of numlist 0 1 {
		foreach b of numlist 0/3 {
			foreach v of varlist childcare primary secondary_* {
				* Secondary childcare isn't defined for children 13+
				if (`b' == 0 | `b' == 3) & regexm("`v'", "secondary") continue

				quietly ivreg2 `v' ib5.month tmspline* i.day if female == `f' & youngest == `b' [aw = wt06], bw($bandwidth) cluster(tm) small
				process_estimates, path("timeuse") model("f`f'_b`b'_`v'")
			}
		}
	}
}


* Prepare estimates
*-------------------------------------------------------------------------------

* Load estimates into memory
load_estimates, path("timeuse")

* Prepare coefficient labels for each month
make_coeflabels


* Show the breakdown of excess childcare time by type of activity
*-------------------------------------------------------------------------------

* Create a dataset of estimates
clear
local filelist

quietly estimates dir
foreach e in `=r(names)' {
	* Turn coefficients into a dataset
	estimates restore `e'
	mat b = e(b)
	clear
	quietly svmat b

	* Add on sex, parental status, and model name
	gen byte female = real(substr("`e'", 2, 1))
	gen byte youngest = real(substr("`e'", 5, 1))
	gen model = substr("`e'", 7, .)

	* Stash for appending
	tempfile `e'
	save ``e''
	local filelist `filelist' ``e''
}

* Stack model estimates
clear
foreach f of local filelist {
	append using `f'
}

reshape long b, i(female youngest model) j(month_adj)
replace month_adj = month_adj - 1
rename month_adj month

* Fill in missing values for secondary childcare
fillin female youngest model month
assert model == "childcare" | regexm(model, "secondary") if _fillin == 1
replace b = 0 if _fillin == 1
drop _fillin

* Verify additivity, up to floating point error
bysort female month: egen btot = total(b * (model == "childcare"))
bysort female month: egen bsub = total(b * (model != "childcare"))
assert abs(btot - bsub) < .0001

* Aggregate up
gcollapse (sum) b, by(female youngest model month)

* Restructure
replace model = "prime" if model == "primary"
replace model = "hhold" if model == "secondary_hhact"
replace model = "leisu" if model == "secondary_leisure"
replace model = "other" if model == "secondary_other"
reshape wide b, i(female youngest month) j(model) string
rename b* *

* Bar positioning
gen curr_pos = 0
gen curr_neg = 0

foreach v in "prime" "hhold" "leisu" "other" {
	gen `v'0 = .
	replace `v'0 = curr_pos if sign(`v') >= 0
	replace `v'0 = curr_neg if sign(`v') < 0

	gen `v'1 = `v'0 + `v'
	replace curr_pos = curr_pos + `v' if `v' > 0
	replace curr_neg = curr_neg + `v' if `v' < 0
}

drop curr_*

* Prepare shading
gen rlow = -5.75
gen rupp = 15
gen rval = month - 0.5

* Create plots
foreach b of numlist 1 2 {
	if `b' == 1 {
		local blbl "und6"
		local col1 "color($col1) fcolor(*1.50)"
		local col2 "color($col2) fcolor(*1.00)"
		local col3 "color($col3) fcolor(*0.75)"
		local col4 "color($col4) fcolor(*0.50)"
	}
	else if `b' == 2 {
		local blbl "6to12"
		local col1 "color(gs5)"
		local col2 "color(gs8)"
		local col3 "color(gs12)"
		local col4 "color(gs16)"
	}

	foreach f of numlist 0 1 {
		if `f' == 0 local flbl "Fathers"
		if `f' == 1 local flbl "Mothers"

		#delimit ;
		twoway
			(rarea rupp rlow rval if female == `f' & youngest == `b' & inrange(month, 0, 5), color($ltgs) plotregion(margin(t=0 b=0 l=0)))
			(rbar prime0 prime1 month if female == `f' & youngest == `b', `col1' fcolor(*1.50) lcolor(black) lwidth(thin))
			(rbar hhold0 hhold1 month if female == `f' & youngest == `b', `col2' fcolor(*1.00) lcolor(black) lwidth(thin))
			(rbar leisu0 leisu1 month if female == `f' & youngest == `b', `col3' fcolor(*0.75) lcolor(black) lwidth(thin))
			(rbar other0 other1 month if female == `f' & youngest == `b', `col4' fcolor(*0.50) lcolor(black) lwidth(thin))
			(connected childcare month if female == `f' & youngest == `b', color(black) msymbol(circle) clwidth(medthick))
			(scatteri 0 -0.5 0 12.5, recast(line) color(black)),
			title("{bf:`flbl'}")
			xtitle("")
			xscale(range(-0.5 12.5))
			xlabel(0 "May" 1 "June" 2 "July" 3 "Aug." 4 "Sept." 5 "Oct." 6 "Nov." 7 "Dec." 8 "Jan." 9 "Feb." 10 "Mar." 11 "Apr." 12 "May", alternate)
			yscale(range(-5.75 15.75))
			ylabel(-5(5)15)
			yline(0)
			legend(rows(2) order(6 2 3 - 4 5) size(*.75)
				label(2 "Primary childcare")
				label(3 "Secondary: household")
				label(4 "Secondary: leisure")
				label(5 "Secondary: other")
				label(6 "Total childcare"));
		#delimit cr

		tempfile g`f'
		graph save "`g`f''"
	}

	grc1leg "`g1'" "`g0'", l1title("Difference relative to May (hours/week)", size(*.8)) imargin(vsmall)
	nicepdf "$basepath/output/timeuse_`blbl'.pdf", panels(2) indirect replace
}

* Report point estimates
estimates replay f1_b2_childcare

* Close the log file
unlaunch
